﻿using ConfigHelper.Common;
using IDataAccessLayer.IBaseInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer.BaseInterface
{
    public abstract class BaseDataAccessLayer<T> : IBaseDataAccessLayer<T> where T : class
    {
        //定义一个抽象的表名
        public abstract string TableName { get; }
        //定义一个抽象的主键
        public abstract Dictionary<string, bool> PKname { get; }


        //登录，显示，反填
        public List<T> Getlist<T>(params SqlParameter[] paramters)
        {
            string sql =Common.GetList<T>(TableName, paramters);
            DataTable dt = DBhelper.GetDataSet(sql);

            List<T> t = new List<T>();
            foreach (DataRow item in dt.Rows)
            {
                Type t1 = typeof(T);

                T t2 = (T)t1.Assembly.CreateInstance(t1.FullName);
                foreach (var i in t1.GetProperties())
                {
                    i.SetValue(t2, item[i.Name]);
                }
                t.Add(t2);

            }
            return t;
        }

        //添加
        public bool ToAddSQL(T t)
        {
            string sql = Common.AddSQL(t, TableName, PKname);
            return DBhelper.GetExecuteNonQuery(sql);
        }

        //删除
        public bool ToDeleteSQL(T t)
        {
            string sql = Common.DeleteSQL(t, TableName, PKname);
            return DBhelper.GetExecuteNonQuery(sql);
        }

        //修改
        public bool ToUpdateSQL(T t)
        {
            string sql = Common.UpdateSQL(t, TableName, PKname);
            return DBhelper.GetExecuteNonQuery(sql);
        }

       
    }
}
